package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.TrainingCheckPoint;
import com.think.teaching.service.impl.TrainingCheckPointServiceImpl;
import com.think.teaching.service.impl.TrainingServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 实训计分点 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "实训检查点配置管理接口")
public class TrainingCheckPointController {
	
	private final TrainingCheckPointServiceImpl trainingCheckPointService;

	private final TrainingServiceImpl trainingServiceImpl;

	public TrainingCheckPointController(TrainingCheckPointServiceImpl trainingCheckPointService, TrainingServiceImpl trainingServiceImpl) {
		this.trainingCheckPointService = trainingCheckPointService;
		this.trainingServiceImpl = trainingServiceImpl;
	}

	@ApiOperation("添加新的实训计分点")
	@PostMapping("admin/checkPoint")
	public Result AddNewTrainingCheckPoint(@RequestBody()TrainingCheckPoint data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingCheckPoint trainingCheckPoint = trainingCheckPointService.AddTrainingCheckPoint(data);
			if (trainingCheckPoint != null) {
				return Result.success(trainingCheckPoint);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("更新实训计分点")
	@RequestMapping(value = "admin/checkPoint",method = RequestMethod.PUT)
	public Result UpdateTrainingCheckPoint(@RequestBody()TrainingCheckPoint data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			TrainingCheckPoint trainingCheckPoint = trainingCheckPointService.UpdateTrainingCheckPoint(data);
			if (trainingCheckPoint != null) {
				return Result.success(trainingCheckPoint);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("删除实训计分点")
	@RequestMapping(value = "admin/checkPoint/{id}",method = RequestMethod.DELETE)
	public Result DeleteTrainingCheckPoint(@PathVariable("id")Integer id) {
		TrainingCheckPoint trainingCheckPoint = trainingCheckPointService.getById(id);
		if (trainingCheckPoint == null) {
			return Result.failure(ResultCode.TRAINING_CHECK_POINT_NOT_EXIST);
		} else {
			if (trainingCheckPointService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取实训计分点详情")
	@RequestMapping(value = "admin/checkPoint/{id}",method = RequestMethod.GET)
	public Result GetTrainingCheckPoint(@PathVariable("id")Integer id) {
		TrainingCheckPoint trainingCheckPoint = trainingCheckPointService.getById(id);
		if (trainingCheckPoint == null) {
			return Result.failure(ResultCode.TRAINING_CHECK_POINT_NOT_EXIST);
		} else {

			return Result.success(trainingCheckPoint);
		}
	}
	@ApiOperation("管理员修改实训计分点状态")
	@RequestMapping(value = "admin/checkPoint/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateCheckPointState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		TrainingCheckPoint trainingCheckPoint = trainingCheckPointService.getById(id);
		if (trainingCheckPoint == null) {
			return Result.failure(ResultCode.TRAINING_CHECK_POINT_NOT_EXIST);
		} else {
			try {
				trainingCheckPoint = trainingCheckPointService.SetTrainingCheckPointState(id, state);
				return Result.success(trainingCheckPoint);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("管理员获取实训考核点列表")
	@RequestMapping(value = "admin/checkPoints/{id}",method = RequestMethod.GET)
	public Result AdminListTraining(
			@PathVariable("id")Integer id) {
		if (trainingServiceImpl.getById(id) == null) {
			return Result.failure(ResultCode.TRAINING_NOT_EXIST);
		}
		return Result.success(trainingCheckPointService.listCheckPointByTrainingId(id, false));
	}
	
	@ApiOperation("用户获取实训考核点列表")
	@RequestMapping(value = "user/checkPoints/{id}",method = RequestMethod.GET)
	public Result UserListTraining(
			@PathVariable("id")Integer id) {
		return Result.success(trainingCheckPointService.listCheckPointByTrainingId(id, true));
	}
}

